package com.baidu.service.impl;

import com.baidu.entity.Auth;
import com.baidu.mapper.AuthMapper;
import com.baidu.response.Response;
import com.baidu.service.AuthService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class AuthServiceImpl implements AuthService {

    @Autowired
    private AuthMapper authMapper;

    @Override
    public Response<List<Auth>> selectAuthByUserCode(String userCode) {
        List<Auth> authList = authMapper.selectAuthByUserCode(userCode);
        Map<Integer, Auth> authMap = new HashMap<>();
        for (Auth auth : authList) {
            authMap.put(auth.getAuthId(), auth);
        }
        List<Auth> rootAuthList = new ArrayList<>();
        for (Auth auth : authList) {
            if (auth.getParentId() == 0) {
                rootAuthList.add(auth);
            } else {
                Auth parentAuth = authMap.get(auth.getParentId());
                if (parentAuth != null) {
                    parentAuth.getChildAuth().add(auth);
                }
            }

        }
        return Response.success(200, "查询成功", rootAuthList);

    }
}
